package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.pojo.User;
import com.ruoyi.common.core.cg.result.Queryparam;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.mapper.UserMapper;
import com.ruoyi.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 马景昌
 * @create 2025/03/06
 * @description
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Result pageshow(User user) {
//        if(pageNum==0 && pageSize==0){
//            pageNum=1;
//            pageSize=5;
//        }
//        根据id查询如果有id不进行分页
            PageHelper.startPage(user.getPageNum(),user.getPageSize());
//        分页查询
            List<User> pageshow = userMapper.pageshow(user);
            for (User usera : pageshow) {
                Integer integer = userMapper.selectOrder0(usera.getUserId());
                usera.setOrderNum0(integer);
                Integer integer1 = userMapper.selectOrder1(usera.getUserId());
                usera.setOrderNum1(integer1);
                BigDecimal sumprice = orderMapper.sumprice(usera.getUserId());
                if(sumprice==null){
                    sumprice=new BigDecimal(0);
                }else {
                    usera.setSpendingAmount(sumprice);
                }
            }
            PageInfo<User> pageInfo = new PageInfo<>(pageshow);

            System.out.println(pageInfo);

        return new Result(true,"查询成功",pageInfo);
    }

    @Override
    public Result update(User user) {
        boolean update = userMapper.update(user);
        if(!update){
            return new Result(false,"修改失败");
        }
        return new Result(true,"修改成功");
    }

    @Override
    public Result hx(Integer userId) {
        if(userId==null){
            return new Result(false,"参数错误");
        }
        User user = userMapper.selectbyId(userId);
        Integer integer = userMapper.selectOrder0(userId);
        user.setOrderNum0(integer);
        Integer integer1 = userMapper.selectOrder1(userId);
        user.setOrderNum1(integer1);
        return new Result(true,"查询成功",user);
    }

    @Override
    public Result delete(Integer userId) {
        if(userId==null){
            return new Result(false,"参数错误");
        }
        boolean deletebyid = userMapper.deletebyid(userId);
        if(!deletebyid){
            return new Result(false,"删除失败");
        }
        return new Result(true,"删除成功");
    }

    @Override
    public Result add(User user) {
        boolean add = userMapper.add(user);
        if(!add){
            return new Result(false,"添加失败");
        }
        return new Result(true,"添加成功");
    }

    @Override
    public PageInfo<User> routerSelect(Queryparam queryparam) {
        PageHelper.startPage(queryparam.getPageNum(),queryparam.getPageSize());
        List<User> user = userMapper.routerSelect(queryparam);
        for (User usera : user) {
            Integer integer = userMapper.selectOrder0(usera.getUserId());
            usera.setOrderNum0(integer);
            Integer integer1 = userMapper.selectOrder1(usera.getUserId());
            usera.setOrderNum1(integer1);
            BigDecimal sumprice = orderMapper.sumprice(usera.getUserId());
            if(sumprice==null){
                sumprice=new BigDecimal(0);
            }else {
                usera.setSpendingAmount(sumprice);
            }
        }
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return pageInfo;

    }


}
